package com.sm.appbase.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * BaseActivity
 * by Forty'7 on 2019/6/12
 */
public abstract class BaseActivity extends AppCompatActivity {

    public Context mContext;
    private Unbinder unBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mContext = this;
        XLog.i("BaseActivity", getClass().getSimpleName());
        unBinder = ButterKnife.bind(this);
        ActivityTaskManager.getInstance().putActivity(getClass().getSimpleName(), this);
        initView();
        initData();
    }

    public Bundle getBundle(Map<String, Object> args) {
        // 通过bundle传递数据
        // 通过bundle传递数据
        Bundle bundle = new Bundle();
        for (Map.Entry<String, Object> entryset : args.entrySet()) {
            if (entryset.getValue() instanceof Integer) {
                bundle.putInt(entryset.getKey(), (Integer) entryset.getValue());
            } else if (entryset.getValue() instanceof String) {
                bundle.putString(entryset.getKey(), (String) entryset.getValue());
            } else if (entryset.getValue() instanceof Long) {
                bundle.putLong(entryset.getKey(), (Long) entryset.getValue());
            } else if (entryset.getValue() instanceof Double) {
                bundle.putDouble(entryset.getKey(), (Double) entryset.getValue());
            } else if (entryset.getValue() instanceof Float) {
                bundle.putFloat(entryset.getKey(), (Float) entryset.getValue());
            } else if (entryset.getValue() instanceof Boolean) {
                bundle.putBoolean(entryset.getKey(), (Boolean) entryset.getValue());
            } else if (entryset.getValue() instanceof Integer) {
                bundle.putInt(entryset.getKey(), (Integer) entryset.getValue());
            } else if (entryset.getValue() instanceof Serializable) {
                bundle.putSerializable(entryset.getKey(), (Serializable) entryset.getValue());
            } else if (entryset.getValue() instanceof Parcelable) {
                bundle.putParcelable(entryset.getKey(), (Parcelable) entryset.getValue());
            }
        }
        return bundle;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    /**
     * 初始化layouy
     */
    protected abstract int getLayoutId();

    /**
     * 初始化布局
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    public void initData() {
    }


    @Override
    protected void onDestroy() {
        unBinder.unbind();
        ActivityTaskManager.getInstance().removeActivity(getClass().getSimpleName());
        super.onDestroy();
    }


}
